package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.controller.base.BaseController;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.base.Json;
import com.mvc.model.base.SessionInfo;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.service.healthmanager.BloodPressureServiceI;

@Controller
@RequestMapping("/blood")
public class bloodPressureController extends BaseController {
	@Autowired
	private BloodPressureServiceI bpService;
	/**
	 * 跳转健康数据血压模块
	 * @param request
	 * @return
	 */
	@RequestMapping("/index")
	@ResponseBody
	public String index(BloodPressure bPressure,HttpSession session){	
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
		if(sessionInfo!=null){
			bPressure.setPersonId(sessionInfo.getPersonId());
			List<BloodPressure> blist = bpService.getMaxBloodPressure(bPressure);
			for(BloodPressure bs : blist){
				session.setAttribute("maxlist",bs);
			}	
		}
		return "/bloodPressure";
	}
	
	/**
	 * 查询个人历次血压信息
	 * @param session
	 * @return 
	 */
	@RequestMapping("/getbloodpressure_old")
	@ResponseBody
	public List<BloodPressure> getBloodPressure(BloodPressure bPressure,HttpSession session){
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
		if(sessionInfo!=null){
			bPressure.setPersonId(sessionInfo.getPersonId());
		}
		return bpService.getBloodPressure(bPressure);		
	}
	
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Json saveOrUpdate(HttpServletRequest request, HttpServletResponse response)  
				throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		System.out.println("已转化数据：" + json);
		BloodPressure bp = new BloodPressure();
		if (!json.isEmpty()) {
			bp = JSON.parseObject(json, BloodPressure.class);
			bp.setId(null);
		}
		BloodPressure bpresure = bpService.saveOrUpdate(bp);
		Json j = new Json();
		if(bpresure!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(bpresure);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping(value="/saveOrUpdate", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveOrUpdate(@RequestBody List<BloodPressure> bps, HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("未转换数据："+JSON.toJSONString(bps));
		Json j = new Json();
		List<BloodPressure> list = new ArrayList<BloodPressure>();
		if(bps.size()>0){
			for(BloodPressure bp : bps){
				Long appLocalId = ValidatorUtil.isNullOnLong(bp.getId())?null:bp.getId();
				bp.setId(null);
				BloodPressure bpresure = new BloodPressure();
				bpresure = bpService.saveOrUpdate(bp);
				if(bpresure!=null&&!ValidatorUtil.isNullOnLong(bpresure.getId())){
					bpresure.setRespCode(0);
				}else{
					bpresure.setRespCode(-1);
				}
				bpresure.setAppLocalId(appLocalId);
				list.add(bpresure);
			}
			j.setMsg("数据已经处理！");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/getNewBloodPressure")
	@ResponseBody
	public Json getNewBloodPressure(BloodPressure bp){
		Json j = new Json();
		BloodPressure bpresure = bpService.getNewBloodPressure(bp);
		if(!ValidatorUtil.isNullOnLong(bpresure.getId())){
			j.setMsg("获取成功");
			j.setSuccess(true);
			j.setObj(bpresure);
		}else{
			j.setMsg("获取数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
}
